import { Module } from 'vuex'
import { getDicts } from '@/api/modules/dict'

interface State {
  form: formInfo //基本数据
  dicts: any //字典数据
  backArr: any //回库数组
}

interface formInfo {
  inOrderCode: string
  theme: string
  operName: string
  auditName: string
  sourceType: string
  tenantId: string
  summary: string
}

const store: Module<State, unknown> = {
  namespaced: true,
  state() {
    return {
      form: {
        inOrderCode: '',
        theme: '',
        operName: '',
        auditName: '',
        sourceType: '',
        tenantId: '',
        summary: '',
      },
      dicts: {},
      backArr: [],
    }
  },
  getters: {
    getFormInfo(state) {
      return state.form
    },
  },
  mutations: {
    setFromInfo(state, value) {
      state.form = value
    },
    setBackArr(state, value) {
      state.backArr = value
    },
  },
  actions: {
    // 缓存出库字典数据
    async setDicts({ state }, value) {
      const arr = [
        'out_order_status',
        'out_order_source_type',
        'wharf_status',
        'out_type',
        'stock_status',
        'station_status',
        'stock_status_type',
        'vmi_status',
      ]
      for (const item of arr) {
        const { data, map } = await getDicts(item)
        state.dicts[item] = { arr: data.map((item) => ({ ...item, value: +item.value })), map }
      }
    },
  },
  modules: {},
}

export default store
